package com.aacar.op.service.washer;


import com.aacar.op.dao.washer.WasherDao;
import com.aacar.op.entity.washer.Washer;
import com.aacar.op.utils.DefineAppConfig;
import com.aacar.op.utils.web.PageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("washerService")
@Transactional
public class WasherServiceImpl implements WasherService {
	
	private static Logger logger = LoggerFactory.getLogger(WasherServiceImpl.class);
	
    @Autowired
    private WasherDao washerDao;

	@Override
	public Washer get(Long id) {
		Washer washer =  washerDao.findOne(id);
		if(!"".equals(washer.getHeadUrl())) {
            washer.setHeadUrl(DefineAppConfig.washer_header_host + washer.getHeadUrl());
        }
		return washer;
	}


	@Override
	public int saveOrUpdate(Washer entity) {
		int ret;
        entity.auditObject();
        if (entity.isNew()) {
			entity.setLoginPwd("123456");
			entity.setIsValid("1");
			entity.setRankId("1");
			entity.setRankName("普通洗车工");
			entity.setCreateTime(new Date());
            ret = washerDao.insert(entity);
        	ret = -1;
        } else {
        	entity.setModifyTime(new Date());
            ret = washerDao.update(entity);
        }
        return ret;
	}

	@Override
	public int delete(Long id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public PageInfo<Washer> getPage(Map<String, Object> params, Pageable pageable) {
		PageUtils.mergeSort(params, pageable);
        //pageNum(页码)从1开始,spring data common domain从0开始的.
        PageHelper.startPage(pageable.getPageNumber() + 1, pageable.getPageSize());
        List<Washer> pageContent = washerDao.findAll(params);
        return new PageInfo(pageContent);
	}

	@Override
	public List<Washer> getAllWasher(Washer entity) {
		Map<String, Object> params = new HashMap<String,Object>();
		params.put("status", "1"); // 状态 1 可用 2停用
		List<Washer> all = washerDao.findAll(params);
		return all;
	}


}
